<?php

class ClientBusiness {

	public function newUser($form, $address)
	{
		try {
			$db = Zend_Db_Table::getDefaultAdapter();

			$insert = $db->insert('cl_client', array(
			'name'     => $form['name'],
			'email'    => $form['email'],
			'username' => $form['login'],
			'password' => md5($form['password']),
			'cpf' => $form['cpf'],
			'phone' => $form['phone'],
			'celphone' => $form['cel'],
			'birth' => $form['birth'],
			'sex' => $form['sex'],
			'user_type' => $form['user_type'],
			));
			$id_client = $db->lastInsertId();
			$insert = $db->insert('cl_address', array(
			'id_client' => $id_client,
			'city' => $address['city'],
			'neighborhood' => $address['neighborhood'],
			'address' => $address['address'],
			'zipcode' => $address['zipcode'],
			'number' => $address['number'],
			'type' => $address['type'],
			'comp' => $address['comp'],
			'ref' => $address['ref'],
			'state' => $address['state'],
			'country' => $address['country'],
			));
				
			return $id_client;
		} catch(Exception $e) {
			Zend_Registry::get('log')->err('newUser() Erro ao cadastrar usuário: '.$e.'');
		}

	}

	public function editLogin($data){
		$db = Zend_Db_Table::getDefaultAdapter();

		$id = $data['id_client'];
		$where = $db->quoteInto ( 'id_client' . ' = ?', $id );

		$update = $db->update ('cl_client', array(
			'email'    => $data['email'],
			'username' => $data['login'],
		), $where);
	}

	public function editClient($data){

		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$id = $data['id_client'];

			$where = $db->quoteInto ( 'id_client' . ' = ?', $id );
			$update = $db->update ('cl_client', array(
			'name'     => $data['name'],
			'cpf' => $data['cpf'],
			'phone' => $data['phone'],
			'celphone' => $data['cel'],
			'birth' => $data['birth'],
			'sex' => $data['sex'],
			), $where);
		}catch (Exception $e){

		}
	}

	public function editAddress($data){

		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$id = $data['id_client'];

			$where = $db->quoteInto ( 'id_client' . ' = ?', $id );
			$update = $db->update('cl_address', $data, $where);

		}catch (Exception $e){

		}
	}

	public function editUser($form, $address)
	{
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$id = $form['id_client'];

			$where = $db->quoteInto ( 'id_client' . ' = ?', $id );
			$update = $db->update ('cl_client', array(
			'name'     => $form['name'],
			'email'    => $form['email'],
			'username' => $form['login'],
			'cpf' => $form['cpf'],
			'phone' => $form['phone'],
			'celphone' => $form['cel'],
			'birth' => $form['birth'],
			'sex' => $form['sex'],
			), $where);

			$update = $db->update('cl_address', array(
			'id_client' => $id,
			'city' => $address['city'],
			'neighborhood' => $address['neighborhood'],
			'address' => $address['address'],
			'zipcode' => $address['zipcode'],
			'number' => $address['number'],
			'type' => $address['type'],
			'comp' => $address['comp'],
			'ref' => $address['ref'],
			'state' => $address['state'],
			'country' => $address['country'],
			), $where);

		}catch(Exception $e){
			Zend_Registry::get('log')->err('editUser() Erro ao editar usuário: '.$id.' '.$e.'');
		}
			
	}
	
	public function changePass($data){
		
		try {
			$db = Zend_Db_Table::getDefaultAdapter();
				
			$where = $db->quoteInto ( 'id_client' . ' = ?', $data['userId'] );

			$update = $db->update ('cl_client', array(
			'password'    => md5($data['new']),
			), $where);
			
		} catch (Exception $e) {
			Zend_Registry::get('log')->err('activeClient() Erro ao ativar usuário id: '.$id.' ' .$e.'');
		}
	 
	}

	public function getUserByLogin($login){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchRow("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.username = ?", $login);

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByLogin() Erro ao recuperar usuário login: '.$login.' '.$e.'');
		}
	}

	public function getUserByLoginOtherId($form){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchAll("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.username = '".$form['login']."' and cc.id_client <> ".$form['id_client']." ");

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByLogin() Erro ao recuperar usuário login: '.$form['login'].' e id: '.$form['id_client'].' '.$e.'');
		}
	}

	public function getUserByEmail($email){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchRow("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.email = ?", $email);

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByEmail() Erro ao recuperar usuário email: '.$email.' '.$e.'');
		}

	}

	public function getUserByEmailOtherId($form){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchAll("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.email = '".$form['email']."' and cc.id_client <> ".$form['id_client']." ");

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByLogin() Erro ao recuperar usuário email: '.$form['email'].' e id: '.$form['id_client'].' '.$e.'');
		}
	}

	public function getUserByCpf($cpf){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchRow("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.cpf = ?", $email);

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByEmail() Erro ao recuperar usuário email: '.$email.' '.$e.'');
		}

	}

	public function getUserByCpfOtherId($form){

		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchAll("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.cpf = '".$form['cpf']."' and cc.id_client <> ".$form['id_client']." ");

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByLogin() Erro ao recuperar usuário cpf: '.$form['cpf'].' e id: '.$form['id_client'].' '.$e.'');
		}
	}

	public function getUserByid($id){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchRow("SELECT * FROM cl_client cc INNER JOIN cl_address ca on ca.id_client = cc.id_client WHERE cc.id_client = ?", $id);

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getUserByid() Erro ao recuperar usuário pelo id : '.$id.' '.$e.'');
		}

	}

	public function getAddressByUser($id){
		try{
			$db = Zend_Db_Table::getDefaultAdapter();

			$select = $db->fetchRow("SELECT * FROM cl_address where id_client = ?", $id);

			return $select;
		}catch (Exception $e){
			Zend_Registry::get('log')->err('getAddressByUser() Erro ao recuperar endereço de usuário: '.$id.' ' .$e.'');
		}
	}

	public function activeClient($id){
		try {
			$db = Zend_Db_Table::getDefaultAdapter();
				
			$where = $db->quoteInto ( 'id_client' . ' = ?', $id );

			$update = $db->update ('cl_client', array(
			'status'    => 1,
			), $where);
			
		} catch (Exception $e) {
			Zend_Registry::get('log')->err('activeClient() Erro ao ativar usuário id: '.$id.' ' .$e.'');
		}
	}
}